<?php
/**
+-----------------------------------------------------------------------+
* @autor 张涛 <tonera@gmail.com>;
* @since 2006-8-17
* @version $Id: act_RecordDkpByLogBD.php,v 1.3.2 tonera$
* @description	admin
* @last update 2006-8-17
+-----------------------------------------------------------------------+
*/
include_once CHINO_LIBPATH.'/db_mysql.inc.php';

@set_time_limit(0);
class RecordDkpByLogBD extends Base {
	var $adm		= '';
	var $gconfig	= array();
	var $timezone	= 0;
	var $syslog		= '';
		//boss kill
	function RecordDkpByLogBD() {
		include_once(CHINO_LIBPATH . "/class_Syslog.php");
		$this->syslog	= new Syslog($GLOBALS['adodb'],$_SESSION['wowdkp']['userid'],$_SESSION['wowdkp']['user'],TABLEHEAD);
		//是否勾选 aispt
		setcookie("wow_cfg_ispt",$_POST['aispt'],time()+2592000);
		include_once(CURRENTPATH . "/class_admin.php");
        $this->adm = new admin;
		$gconfigarr	= $this->adm->getInfoByTable('config');
		if(is_array($gconfigarr)) {
			foreach($gconfigarr as $key=>$val) {
				$this->gconfig[trim($val['vname'])]	= trim($val['value']);
			}
		}
		$this->timezone	= (float)$this->gconfig['timezone'] * 3600 - date("Z");
		//公共物品入库
		$pitemdkp	= 0;
		$ptotal		= 0;
		$cid		= (int)$_POST['cid'];
		$etid		= (int)$_POST['etid'];
		$distime	= date("Y-m-d H:i:s",time() + $this->timezone);
		$members	= array();
		$lastraidtime	= date("Y-m-d",strtotime($_POST['eventTime']));
		$this->adm->_adminCheck(5);
		
		/*
		1.全体会员 array(uname=>uid)
		2.有效会员 array(uname=>uid)
		3.如果+物品价值均值，则计算 array(item=>array(uid,dkp,num,color,boss))
		3.分boss计算会员dkp(如果+物均，那么计算物品dkp)
		4.插入事件 
		5.写入会员dkp分配明细
		6.会员dkp数据更新
		*/
		$allUser		= array();
		$validUser		= array();
		$userDkp		= array();		//有dkp的用户
		$nobossItem		= array();		//没有发现归哪个boss的物品
		$bossMembers	= array();		//参与击杀boss的会员
		$nobossDkp		= 0;
		$totalMember	= 0;			//总共有分的会员数
		$_POST['members']	= is_array($_POST['members'])?$_POST['members']:array();
		$_POST['raidmember']= is_array($_POST['raidmember'])?$_POST['raidmember']:array();
		$_POST['aitemname']= isset($_POST['aitemname'])?$_POST['aitemname']:array();
		foreach($_POST['members'] as $key=>$val) {
			$allUser[$val]	= $_POST['memberuid'][$key];
		}
		foreach($_POST['raidmember'] as $key=>$val) {
			$tmp	= explode("\n",$val);
			foreach($tmp as $k=>$v) {
				$uname	= trim($v);
				if(empty($uname)) continue;
				if(empty($allUser[$uname])) exit("您指定的会员并不存在于这次活动");
				$validUser[$uname]		= $allUser[$uname];
				$bossMembers[$key][]	= $uname;
			}
		}
		if($_POST['aispt']) {
			foreach($_POST['aitemname'] as $key=>$val) {
				$uname		= $_POST['aitemplayer'][$key];
				$dkp		= $_POST['aitemcosts'][$key];
				$color		= $_POST['aitemcolor'][$key];
				$itemid		= $_POST['aitemid'][$key];
				$num		= $_POST['aitemcount'][$key];
				$toboss		= $_POST['toboss'][$key];
				$uid		= $allUser[$uname];
				if(!$uid) exit("您指定的会员并不存在于这次活动");
				$nobossItem[]	= array($val, $uname, $dkp, $color, $itemid, $num, $toboss, $uid);
			}
		}
		$totalMember	= count($validUser);
		if(empty($totalMember)) exit("本次活动无人参与");
		if($_POST['aispt']) {
			foreach($nobossItem as $key=>$val) {
				if($val[6] == 'all') {
					$nobossDkp	+= $val[2];				//noboss物品价值总和
				}
			}
			$avgDkp		= $nobossDkp/$totalMember;			//noboss物品价值均值
		}else {
			$avgDkp		= 0;
		}
		
		//分boss计分
		foreach($_POST['boss'] as $key=>$val) {
			$alldis		= (float)$_POST['dis'][$key];		//全体调节分
			$ispt		= $_POST['ispt'][$key];		//是否按物品均分算
			$itemTotal	= 0;						//本次事件中物品价值总和
			if($ispt) {
				//物品统计
				foreach($_POST['itemname'][$key] as $k=>$v) {
					$uname		= $_POST['itemplayer'][$key][$k];
					$dkp		= (float)$_POST['itemcosts'][$key][$k];
					$color		= $_POST['itemcolor'][$key][$k];
					$itemid		= $_POST['itemid'][$key][$k];
					$num		= $_POST['itemcount'][$key][$k];
					$toboss		= $_POST['itemboss'][$key][$k];
					$uid		= $allUser[$uname];
					if(empty($uid)) continue;			//分解或者仓库之类的。丢弃
					$nobossItem[]	= array($v, $uname, $dkp, $color, $itemid, $num, $toboss, $uid);
					$itemTotal	+= $dkp;
				}
			}
			$eventMemberNum	= count($bossMembers[$key]);
			if(empty($eventMemberNum)) continue;
			$AvgItem	= $itemTotal/$eventMemberNum;		//此事件物品平均分
			foreach($bossMembers[$key] as $k=>$v) {
				$uid	= $allUser[$v];
				if(empty($uid)) continue;
				$userDkp[$uid]	= $alldis+$AvgItem;
			}
		}

		$sql	= "insert into ".TABLEHEAD."_event (name,notes,etid,raidtime,cid) values('$_POST[raidname]','$_POST[notes]','$etid','$lastraidtime','$cid')";
		$GLOBALS['adodb']->execute($sql);
		$eid		= $GLOBALS['adodb']->Insert_ID();
		$this->syslog->writeLog($GLOBALS['oplog']['a_event'],$_POST['allaverevent'],"ID:".$eid);
		//此事件副本数+1
		$GLOBALS['adodb']->execute("update ".TABLEHEAD."_copy set raidtotal=raidtotal+1 where id = '$cid'");
		$validUid	= array();
		
		foreach($userDkp as $key=>$val) {
			$dkp	= $val+$avgDkp;
			$sql	= "update ".TABLEHEAD."_user set lastraidtime='$lastraidtime' where id='$key'";
			$GLOBALS['adodb']->execute($sql);
			$sql	= "insert into ".TABLEHEAD."_itemdis(iid,eid,uid,value,distime,stat,cid) values(0,'$eid','$key','$dkp','$distime','1','$cid')";
			$GLOBALS['adodb']->execute($sql);
			$validUid[$key]	= 1;
		}
		foreach($nobossItem as $key=>$val) {
			$name		= $val[0];
			$itemid		= $val[4];
			$icolor		= $val[3];
			$value		= $val[2];
			$uid		= $val[7];
			$sql		= "insert into ".TABLEHEAD."_item (eid,name,stat,intotime,ipid,icolor) values('$eid','$name','1','$distime','$itemid','$icolor')";
			$GLOBALS['adodb']->execute($sql);
			$iid		= $GLOBALS['adodb']->Insert_ID();
			$sql		= "insert into ".TABLEHEAD."_itemdis (iid,eid,uid,value,distime,stat,cid) values('$iid','$eid','$uid','$value','$distime','-1','$cid')";
			$GLOBALS['adodb']->execute($sql);
			$validUid[$uid]	= 1;
		}
		foreach($validUid as $key=>$val) {
			$this->adm->_accountdkp($key);		//刷新dkp
			if($_POST['etid'] == '1') {			//给每个成员添加一次raid次数
				$sql	= "update ".TABLEHEAD."_dkpvalues set raidnum=raidnum+1 where uid='$key' and copyid='$cid'";
				$GLOBALS['adodb']->execute($sql);
			}
		}

		$this->throwInfo($GLOBALS['lang']['note26'],"index.php?module=admin&act=ShowRaidLogForm");
	}//end function

	function throwInfo($info, $goto='', $target='self') {
		//var_dump($GLOBALS['smarty']);
        $GLOBALS['smarty']->assign('info', $info);
        $GLOBALS['smarty']->assign('goto', $goto);
        $GLOBALS['smarty']->assign('target', $target);
        $GLOBALS['smarty']->display('info.htm');
        exit;
	}
}